//
//  YTProfileViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/4/27.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTProfileViewController.h"
#import "YTAccountSecurityViewController.h"
#import "YTProfileViewModel.h"
#import "YTProfileModel.h"

@interface YTProfileViewController ()
@property (strong, nonatomic, readwrite) YTProfileViewModel *viewModel;
@property (weak, nonatomic) IBOutlet UIImageView *avatarImage;
@property (weak, nonatomic) IBOutlet UILabel *userNameText;
@property (weak, nonatomic) IBOutlet UILabel *userIdText;
@property (weak, nonatomic) IBOutlet UIButton *authStatusBtn;
@property (weak, nonatomic) IBOutlet UILabel *driverStarText;
@property (weak, nonatomic) IBOutlet UILabel *driverScoreText;
@property (weak, nonatomic) IBOutlet UILabel *violationText;
@property (weak, nonatomic) IBOutlet UILabel *driverLVText;
@property (weak, nonatomic) IBOutlet UILabel *moneyText;
@property (weak, nonatomic) IBOutlet UILabel *accountNumText;
@property (weak, nonatomic) IBOutlet UILabel *carManagerText;
@end

@implementation YTProfileViewController

- (void)bindViewModel {
    [super bindViewModel];
}

- (void)initialize {
    self.naviType = YTNavigationBarDefaultStyle;
    self.viewModel = [[YTProfileViewModel alloc] init];
    @weakify(self);
    [[[NSNotificationCenter defaultCenter]
      rac_addObserverForName:YTUserLoginSuccessNotification object:nil]
      subscribeNext:^(id x) {
          @strongify(self);
          [self.viewModel.loadUserInfoCommand execute:nil];
      }];
    
    [[self rac_signalForSelector:@selector(viewWillAppear:)] subscribeNext:^(id x) {
        @strongify(self);
        [self.viewModel.loadUserInfoCommand execute:nil];
    }];
    
    [[_viewModel.loadUserInfoCommand.executionSignals switchToLatest] subscribeNext:^(YTProfileModel *model) {
        @strongify(self);
        [self loadUserInfoWithModel:model];
    } error:^(NSError *error) {
        NSLog(@"%@", error.description);
    }];
    
    [[_viewModel.loadUserInfoCommand.executing take:3] subscribeNext:[self yt_showHudWithTips:nil]];
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return section == 0 ? 0.01 : 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 5) return 10;
    return 5;
}

#pragma mark - Private

- (void)loadUserInfoWithModel:(YTProfileModel *)model {
    _userNameText.text = model.userName;
    _userIdText.text = [NSString stringWithFormat:@"UID: %@", model.userId];
    _driverStarText.text = [NSString stringWithFormat:@"%.1f", [model.serviceScore floatValue]];
    _driverScoreText.text = model.driverPoints;
    _violationText.text = model.misdeedTimes;
    _driverLVText.text = model.driverClassName;
    _moneyText.text = model.accountRemain;
    _accountNumText.text = [NSString stringWithFormat:@"%zd", model.accountCount];
    [_avatarImage yy_setImageWithURL:[NSURL URLWithString:model.userFace]
                         placeholder:[UIImage imageNamed:@"profile_avatar"]];
    switch (model.userRoleNew) {
        case 1:
            [_authStatusBtn setTitle:@"未认证" forState:UIControlStateNormal];
            break;
        case 2: {
            [_authStatusBtn setTitle:@"已认证" forState:UIControlStateNormal];
            [_authStatusBtn setTintColor:kBlackColor];
        }
            break;
        case 3:
            [_authStatusBtn setTitle:@"待审核" forState:UIControlStateNormal];
            break;
        case 4:
            [_authStatusBtn setTitle:@"审核中" forState:UIControlStateNormal];
            break;
        case 5:
            [_authStatusBtn setTitle:@"审核未通过" forState:UIControlStateNormal];
            break;
        case 6: {
            [_authStatusBtn setTitle:@"认证车队" forState:UIControlStateNormal];
            [_authStatusBtn setTintColor:kBlackColor];
            _carManagerText.text = @"车队管理";
        }
            break;
        case 7: {
            [_authStatusBtn setTitle:@"地接社" forState:UIControlStateNormal];
            [_authStatusBtn setTintColor:kBlackColor];
        }
            break;
        default:
            break;
    }
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0) {
        switch ([YTUtilityManager manager].userModel.userRoleNew) {
            case 1:
                [self performSegueWithIdentifier:@"YTDriverAuth" sender:nil];
                break;
            case 2:
            case 6:
                [self performSegueWithIdentifier:@"YTModifyInfo" sender:nil];
                break;
            case 3:
            case 4:
                [self performSegueWithIdentifier:@"YTDriverAuthWait" sender:nil];
                break;
            case 5:
                [self performSegueWithIdentifier:@"YTDriverAuthFail" sender:nil];
                break;
        }
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        if ([YTUtilityManager manager].userModel.userRoleNew == 6) {
            [self performSegueWithIdentifier:@"YTTeamManager" sender:nil];
        } else {
            [self performSegueWithIdentifier:@"YTCarsManager" sender:nil];
        }
    }
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"YTAccountSecurity"]) {
        YTAccountSecurityViewController *account = segue.destinationViewController;
        account.authState = _authStatusBtn.currentTitle;
    }
}

@end
